home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 13 / MacFormat n. 13 (Spain) / Macformat 13.bin / Shareware Internet / Newton / picofermibagels-13 folder / picoferm.nwt < prev   
Encoding:
Text File  |  1995-12-31  |  11.7 KB  |  431 lines

  1. Notes
  2. {labels: 'NIL, viewFont: 10241} // nil=Unfiled, or specify a folder, e.g., 'Business. remove viewFont for current Styles default
  3. //ERASE! in order to erase existing entries in this folder first
  4.  
  5. picoferm.nwt -- Slurpee format
  6. 1/1/96
  7. Copyright 1993-96. S. Weyer. All Rights Reserved Worldwide.
  8.  
  9. picoferm.nwt and PFB.pkg are freeware. Newt is shareware.
  10. For further info:
  11.   http://www.netaxs.com/~weyer/newton/releases.html
  12.   mailto:weyer@netaxs.com
  13.  
  14. (optional) see picoferm.bit for Bitmaps: PFBIcon
  15.  
  16. (optional) install ViewCnst.pkg for view constants (e.g., vfFillWhite) and ButtonBounds
  17.  
  18. Newt source version of Pico Fermi Bagels
  19.  
  20. compatible with NOS 2.0
  21. - supports landscape mode
  22.  
  23. package size =~20K
  24. ----------
  25. PFB
  26. //:doObj('build,'PFB)
  27. //:doObj('add,'PFB)
  28. //:doObj('remove,'PFB)
  29. {viewClass: clView,    // add our own status&title rather than use _proto: protoApp
  30. viewBounds: NIL,    // set in viewSetupFormScript
  31. declareSelf: 'base,    // so close box will work
  32. viewFormat: 328017,    // vfFillWhite + vfFrameBlack + vfPen*1 + vfInset*1 + vfShadow*1
  33. viewFlags: 4,        // vApplication
  34. viewJustify: 0,
  35.  
  36. title: "Pico Fermi Bagels",
  37. allowZero: true,    // set by ZeroCheck
  38. answer: nil,
  39. numDigits: 3,        // set by DigitsPicker
  40. win: nil,
  41.  
  42. newBounds: func(view,loffset,toffset,roffset,boffset,xgrid,ygrid)
  43.     begin
  44.     local vb := view.viewBounds;
  45.     local wid := vb.right+roffset-vb.left-loffset;
  46.     local ht := vb.bottom+boffset-vb.top-toffset;
  47.     self.viewBounds := RelBounds(
  48.         loffset,
  49.         toffset,
  50.         if xgrid then Floor(wid/xgrid)*xgrid else wid,
  51.         if ygrid then Floor( ht/ygrid)*ygrid else ht);
  52.     end,
  53. winSound: @102, // ROM_funbeep
  54. newSound: @314, // ROM_poof
  55.  
  56. viewSetupFormScript: func()
  57.     begin
  58.     answer := win := helpView := nil;
  59.     self.PFBview := self;         // for :newBounds in guessExpando
  60.     local ap := GetAppParams(); // configure to screen size
  61.     self.viewBounds := RelBounds(
  62.         ap.AppAreaLeft,ap.AppAreaTop,
  63.         ap.AppAreaWidth,ap.AppAreaHeight);
  64.     end,
  65.  
  66. viewQuitScript: func()
  67.     begin
  68.     if helpView then helpView:close();
  69.     helpView := NIL;
  70.     end,
  71.  
  72. _package: {
  73.     shortTitle: "PFB 1.3",
  74.     icon: :GetPictAsBits("Bitmaps","PFBIcon"),
  75.     },
  76. }
  77. ----------
  78. PFB.helpStuff~
  79. {// these added to support help book (see helpapp1.nwt)
  80. helpBook: :helpBookTemplate(), // Newt creates one
  81. helpView: NIL,
  82.  
  83. openHelp: func()
  84.     if helpBook
  85.     then begin
  86.         if not helpView
  87.         then helpView := BuildContext(
  88.             {_proto: GetRoot().TinyTim._proto,
  89.             bookRef: helpBook});
  90.         GetRoot().TinyTim:Close();   // in case system help is open
  91.         helpView:openManual(helpBook);
  92.         end,
  93. }
  94. ----------
  95. PFB.rotateStuff~
  96. {// to support Rotate for Newton OS 2.0
  97. //reorienting: NIL,
  98. ReorientToScreen: func()
  99.     begin
  100.         //reorienting := TRUE;
  101.         :SyncView();
  102.         :RedoChildren();
  103.         //reorienting := NIL;
  104.     end,
  105. }
  106. ----------
  107. PFB.gameStuff~
  108. {generateAnswer: func()
  109.     begin
  110.     local i, rdig, z0 := if allowZero then 0 else 1, z9 := 9;
  111.     answer := Array (numDigits, nil);
  112.     for i:=0 to numDigits-1
  113.     do begin
  114.         repeat rdig := Random (z0,z9)
  115.         until not SetContains(answer,rdig);
  116.         answer[i] := rdig;
  117.         if rdig=z0 then z0:=z0+1 // squeeze range to minimize subsequent random gen
  118.         else if rdig=z9 then z9:=z9-1;
  119.         end;
  120.     end,
  121. newGame: func()
  122.     begin
  123.     answer := win := nil;
  124.     PlaySound(newSound);
  125.     if guessExpando exists
  126.     then guessExpando:reset();
  127.     end,
  128. showAnswer: func()
  129.     begin
  130.     if win
  131.     then PlaySound(winSound);
  132.     if answerGlance exists
  133.     then begin
  134.         answerGlance.text :=
  135.             if answer
  136.             then (if win then "You won!" else "Too hard?") &&
  137.                 "The answer is" && Stringer(answer)
  138.             else "Hey! You haven't even guessed yet!!";
  139.         answerGlance:open();
  140.         end;
  141.     end,
  142. }
  143. ----------
  144. PFB.evaluateGuess
  145. func(guess) // separate from main PFB app frame for smaller note/easier editing
  146. begin
  147.     local i, g, result, charnum;
  148.     local zeroch := ord($0), fermi:=0, pico:=0;
  149.     local z := if allowZero then 0 else 1;
  150.  
  151.     // if we haven't generated an answer yet, do it now.
  152.     if not answer
  153.     then :generateAnswer();
  154.  
  155.     // destructively remove extraneous spaces, decimal point
  156.     StrReplace(guess," ", "", nil);
  157.     StrReplace(guess,".", "", nil);
  158.  
  159.     // check length of guess
  160.     if StrLen(guess) <> numDigits
  161.     then result := "<not "&numDigits&" digits>";
  162.  
  163.     // check that guess is all digits, with none duplicated
  164.     local aguess := Array(numDigits,nil);
  165.     for i:=0 to min(numDigits,StrLen(guess))-1
  166.     do begin
  167.         g := guess[i];
  168.         charnum := Ord(g)-zeroch; // conv char to digit
  169.         if (charnum < z) or (charnum > 9)
  170.         then result := result& "<"& g &" not "&z&"-9>"
  171.         else if SetContains(aguess,charnum)
  172.         then result := result& "<"& g &" duplicate>"
  173.         else aguess[i]:=charnum; // save for duplicate comparison
  174.  
  175.         // check guess against answer
  176.         if answer[i]=charnum
  177.         then fermi := fermi+1
  178.         else if SetContains(answer,charnum)
  179.         then pico := pico+1;
  180.         end;
  181.  
  182.     if result // return error or feedback
  183.     then return result
  184.     else if fermi=0 and pico=0
  185.     then return "Bagels";
  186.     for i:=1 to fermi do result := result & "Fermi ";
  187.     for i:=1 to pico  do result := result & "Pico ";
  188.     if fermi=numDigits then win:= true;
  189.     result;
  190. end
  191. ----------
  192. ButtonBounds
  193. func(width) // to add application buttons to protoStatus at Newt development time
  194. begin // or install ViewCnst.pkg plug-in to obtain this method
  195.     local left1:=25, top:=2, spacing:=6, bottom:=15;
  196.     if width > 0
  197.     then SetBounds(spacing,  top, spacing + width, bottom)   // other buttons
  198.     else SetBounds(left1,    top, left1 - width,   bottom);  // first button
  199. end
  200. ----------
  201. PFB+myStatus
  202. {_proto: protoStatus, // see protos2.nwt
  203. }
  204. ----------
  205. PFB.myStatus+b1info
  206. {_proto: protoTextButton,
  207. buttonClickScript: openHelp, // basically same as: func() :openHelp(), // copy method ref
  208. text: "Info",
  209. viewBounds: :ButtonBounds(-44), // first application button (see protos2.nwt)
  210. }
  211. ----------
  212. PFB.myStatus+b2giveup
  213. {_proto: protoTextButton,
  214. buttonClickScript: showAnswer, //func() :showAnswer(), // copy method ref
  215. text: "Give Up",
  216. viewBounds: :ButtonBounds(44),
  217. viewJustify: 8389670, // vjParentRightH + vjParentTopV + vjSiblingRightH + oneLineOnly + vjCenterH + vjCenterV
  218. }
  219. ----------
  220. PFB+titleObj
  221. {_proto: protoTitle}
  222. ----------
  223. PFB+ZeroCheck
  224. {_proto: protoCheckbox,
  225. text: "Allow Zero",
  226. valueChanged: func()
  227.     begin
  228.     allowZero := viewValue;
  229.     if answer then :newGame();
  230.     end,
  231. viewBounds: RelBounds(8,17,72,14),
  232. viewSetupFormScript: func()
  233.     begin
  234.     viewValue := allowZero;
  235.     inherited:?viewSetupFormScript();
  236.     end,
  237. viewValue: true, // initial default
  238. }
  239. ----------
  240. PFB+NewGameButton
  241. {_proto: protoTextButton,
  242. buttonClickScript: newGame, //func() :newGame(), // copy method ref
  243. text: "New Game",
  244. viewBounds: RelBounds(0,20,60,10),
  245. viewJustify: 8388630, // vjCenterH + vjCenterV + oneLineOnly + vjParentCenterH
  246. }
  247. ----------
  248. PFB+DigitsPicker
  249. {_proto: protoLabelPicker,
  250. labelCommands: ["2","3","4", "5"],
  251. text: "Digits",
  252. textSetup: func() NumberStr(numDigits),
  253. viewBounds: RelBounds(-66,19,66,14),
  254. viewJustify: 8388640, // vjLeftH + oneLineOnly + vjParentRightH
  255. labelActionScript: func(cmd) // assumes single digit
  256.     begin
  257.     local newDigits := Ord(labelCommands[cmd][0])-Ord($0);
  258.     if numDigits <> newDigits
  259.     then begin
  260.         numDigits := newDigits;
  261.         if guessExpando exists
  262.         then guessExpando:setStyle(numDigits);
  263.         if answer then :newgame();
  264.         end;
  265.     end,
  266. }
  267. ----------
  268. PFB.myGuessEProto
  269. {_proto: protoTextExpando,
  270. entryFlags: 11009, // vVisible+vClickable+vGesturesAllowed+vNumbersAllowed+vSingleUnit +vNoKeys(16)???
  271. viewJustify: 8388624, // vjParentCenterH+vjParentTopV + vjLeftH+vjTopV+oneLineOnly
  272. viewSetupDoneScript: func()
  273.     begin
  274.         entryLine.textFlags := 2; //vNoSpaces;
  275.         inherited:?viewSetupDoneScript();
  276.     end,
  277. // label: "Guess N",
  278. // path: 'guessN,
  279. }
  280. ----------
  281. PFB+GuessExpando
  282. {_proto: protoExpandoShell,
  283. editHeight: 46,
  284. indent: 60,
  285. lineHeight: 14,
  286. //lines: [], // set in viewSetupFormScript
  287. //numLines: 16, // set in viewSetupFormScript
  288. //target: NIL,  // set in viewSetupFormScript
  289. //viewBounds: RelBounds(1,35,238,270), // set via :newBounds in viewSetupFormScript
  290. viewFormat: 336,
  291. viewSetupFormScript: func()
  292.     begin local i, symslot;
  293.     self.target := self;
  294.     :newBounds(PFBview,1,35+editHeight,-1,-27,nil,lineHeight);
  295.     self.numLines := min(16,Floor((viewbounds.bottom-viewbounds.top)/lineHeight));
  296.     viewbounds.top := 35; // viewbounds.top-editHeight
  297.     editWidth := viewBounds.right - viewBounds.left - 20;
  298.     :setStyle(numDigits);
  299.     self.lines := Array(numLines,nil);
  300.     for i:=1 to numLines
  301.     do begin
  302.         symslot := Intern("guess"&i);
  303.         lines[i-1]:=
  304.             {_proto: myGuessEProto, // defined in app
  305.             label: "Guess "&i,
  306.             path: symslot,  // e.g., 'guess1
  307.             };
  308.         SetValue(target,symslot,clone(""));
  309.         end;
  310.     inherited:viewSetupFormScript()
  311.     end,
  312. reset: func()
  313.     begin local fr;
  314.     foreach fr in lines
  315.     do SetValue(target, fr.path, clone(""));
  316.     end,
  317.  
  318. setStyle: func(dig)
  319.     self.textStyle :=
  320.         if dig <=3 or editWidth>240 // don't shrink for landscape
  321.         then 0x00003001  // userFont12
  322.         else if dig=4
  323.         then 0x00002801  // userFont10
  324.         else 0x00002401, // userFont9
  325. flushEdits: func() nil, //inherited:?flushEdits(), // seems to be needed
  326. CloseEdit: func(view)
  327.     begin
  328.     local val := target.(view.path);
  329.     local lab := :evaluateGuess(val);
  330.     SetValue(target, view.path, val && lab);
  331.     if win then :showAnswer();
  332.     end,
  333. }
  334. ----------
  335. PFB+AnswerGlance
  336. {_proto: protoGlance,
  337. text: "Welcome to Pico Fermi Bagels",
  338. viewFlags: 515,  // vVisible must be on to avoid later problems...
  339. viewBounds: RelBounds(0,0,140,60),
  340. viewJustify: 86, // vjParentCenterH+vjParentCenterV + vjCenterH+vjCenterV
  341. }
  342. ----------
  343. PFB.helpBook+page1
  344. .# first page to be added to helpBook. name only used for editing & to determine default build order
  345. .subject 1
  346. Describe PFB
  347. .story
  348. PFB stands for Pico, Fermi, Bagels -- a number guessing game, similar to MasterMind or Jotto.
  349.  
  350. For each guess, you will be told:
  351. - Fermi: a correct digit placed correctly
  352. - Pico: a correct digit placed incorrectly
  353. - Bagels: no digits are correct
  354. ----------
  355. PFB.helpBook+page2
  356. .subject 1
  357. Understand the Code
  358. .story
  359. This version of Pico Fermi Bagels (1.3) includes the Newt source; see: pfb.txt.
  360.  
  361. You could also register for the Newt Development Environment (see "Develop") to obtain a Newton programming manual and more NewtonScript application examples.
  362. ----------
  363. PFB.helpBook+page3
  364. .subject 1
  365. Distribute
  366. .story
  367. PFB is postcardware*. PFB may be distributed freely as long as all of the files are included and unmodified.  You are free to make modifications for your own use.
  368.  
  369. *Dedicated to Kristina and Ellie -- send email to Vader42@aol.com and EllieJelly@aol.com, or postcards or other valuable tokens of appreciation (address in "Contact Author").
  370.  
  371. Copyright 1993-96, S. Weyer. All Rights Reserved Worldwide.
  372. ----------
  373. PFB.helpBook+page4
  374. .subject 1
  375. Use
  376. .story
  377. \uFC0B\u Allow Zero: is zero a possible digit?
  378. [New Game]: start a new game
  379. \uFC01\u Digits: select number of digits (2-5)
  380.  
  381. <write>: guesses
  382. <double-tap>: for numeric keypad
  383. <close/next label>: see result
  384.  
  385. [Info]: this help book
  386. [Give Up]: see answer
  387. ----------
  388. PFB.helpBook+page5
  389. .subject 1
  390. Example
  391. .story
  392. If \uFC0B\u Allow Zero (checked)
  393.    \uFC01\u Digits 3
  394.    PFB's number is 029
  395. 1. 445 <4 duplicate>
  396. 2. 4567 <not 3 digits>
  397. 3. 376 Bagels  {no digits correct}
  398. 4. 914 Pico
  399.   {1 digit correct but in wrong place}
  400. 5. 820 Fermi Pico
  401.   {2 digits correct, but 1 in wrong place}
  402.    . . .
  403.    092 Fermi Pico Pico
  404.    {all digits correct, 2 in wrong places}
  405.    029 Fermi Fermi Fermi {win; sound}
  406. ----------
  407. PFB.helpBook+page6
  408. .subject 1
  409. Develop
  410. .story
  411. If you are interested in object-oriented programming, developing applications in NewtonScript and saving as packages directly on your Newton, try the "Newt" development environment.
  412.  
  413. Registered users receive a 80+ pp. manual (Acrobat or paper) and 190+ examples.
  414.  
  415. For further info, see pfb.txt or http://www.netaxs.com/~weyer/newton/releases.html 
  416. ----------
  417. PFB.helpBook+page7
  418. .subject 1
  419. Contact Author
  420. .story
  421. Steve Weyer
  422. 17 Timber Knoll Drive
  423. Washington Crossing, PA 18977-1052
  424. Internet: weyer@netaxs.com
  425. America Online, eWorld, NewtonMail: SteveWeyer
  426. Compuserve: 74603,2051
  427.  
  428. http://www.netaxs.com/~weyer/newton/releases.html
  429. ----------
  430. BYE!
  431.